<?php

include("connect.php");



function getPublicationName($id, $dblink) {


$query = sprintf("SELECT `Name` FROM Publication WHERE `ID` = '%s'", mysql_real_escape_string($id));

	 $result = mysql_query($query, $dblink);



	

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}
$value = "";
if(mysql_num_rows($result)!=0) {
		$value = mysql_result($result, 0); // outputs first publication's name

}
	return $value;
	

}


function getPersonName($id, $dblink) {


$query = sprintf("SELECT `Surname`, `FirstNames` FROM Person WHERE `ID` = '%s'", mysql_real_escape_string($id));

	 $result = mysql_query($query, $dblink);


	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}
$value = "";
if(mysql_num_rows($result)!=0) {

		$row = mysql_fetch_assoc($result);
		$value = $row['Surname'].", ".$row['FirstNames']; // outputs first persons surname then first names

}
	return $value;
	

}


function UnlinkPubPerson($pubid, $personid, $dblink) {


$query = sprintf("delete from PublicationPerson where `PersonID` = %s and `PublicationID` = %s", mysql_real_escape_string($personid), mysql_real_escape_string($pubid));

	 $result = mysql_query($query, $dblink);


	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}

}


if($_POST['personid'] != "" && $_POST['personid'] != NULL && $_POST['pubid'] != "" && $_POST['pubid'] != NULL) {

UnlinkPubPerson($_POST['pubid'], $_POST['personid'], $link);

header('Location: publicationperson.php?publication='.$_POST['pubid']) ;


} else {

include("header.php");

echo "<form method=\"POST\" action=\"unlinkpersonpublication.php\"><h4>Are you sure you want to remove ".getPersonName($_GET['PersonID'], $link)." from ".getPublicationName($_GET['PubID'], $link)."?</h4>

<input type=\"hidden\" name=\"pubid\" value=\"".$_GET['PubID']."\" /><input type=\"hidden\" name=\"personid\" value=\"".$_GET['PersonID']."\" /><div><input type=\"submit\" id=\"submit\" name=\"submit\" value=\"Yes\" />
<a class=\"submitlinks\" href=\"publicationperson.php?publication=".$_GET['PubID']."\">No</a></div></form></body></html>";


}




?>